﻿using System.Collections.Generic;
using System.Data.SqlClient;
using com.fukefu.sql;
using com.fukefu.tools;

namespace com.fukefu
{
    public class KeywordToTitle
    {
        Queue<Title> queue_keyword = new Queue<Title>();
        KeywordType ktype = new KeywordType();
        ExpandKeyword expand = new ExpandKeyword();

        SqlServerOperate sql = new SqlServerOperate();
        //更新所有关键字
        public void Update()
        {
            LoadKeyword();
            sql.Update("insert into _bt_标题_backup(dt,titleid,kid,word) select dt,titleid,kid,word from _bt_标题");
            sql.Update("truncate table _bt_标题");
            Title k;
            while (queue_keyword.Count > 0)
            {
                k = queue_keyword.Dequeue();
                sql.Update("insert into  _bt_标题(kid,word) values('" + k.KId + "','" + k.Word + "')");
            }
            sql.Close();
        }
        //加载所有关键字
        private void LoadKeyword()
        {
            //加载未发表的关键字
            SqlDataReader re = sql.Query("select * from _bt_关键字");
            string line;
            while (re.Read())
            {
                line = re["word"].ToString() + GetExpandKeyword(re["expandid"].ToString(), re["ktid"].ToString());
                queue_keyword.Enqueue(new Title(re["kid"].ToString(), line));
            }
            re.Close();
        }
        //获取扩展词
        private string GetExpandKeyword(string k1, string k2)
        {
            string line = expand.Get(k1);
            line = line.Replace("xxx", ktype.Get(k2));
            line = line.Replace("aa", ktype.GetShortTypeName(k2));
            return line;
        }
    }
}
